<?php
/**
 * 短链列表
 */
class UriAction extends FrontAction{

    private $url = "http://a.uri.local";

    public function __construct() {
        ini_set("memory_limit", "1024m");
        set_time_limit(60);
        parent::__construct();
    }

    public function doIndex(){
        $this->smarty->display('uri/index.html');
    }
    public function indexList(){
        $pageSize = AppCommon::getRequestInt("limit");
        $page = AppCommon::getRequestInt("page");
        $pageSize = $pageSize > 0 ? $pageSize : 10;
        $respage = $page > 0 ? $page : 1;
        $page = ($respage - 1) * $pageSize;

        $uri_key = AppCommon::getRequestString('uri_key');
        $starttime = AppCommon::getRequestString('starttime');

        $where = array();

        if ($uri_key){
            $where['key'] = $uri_key;
        }
        if ($starttime){
            $starttime = explode(' - ',$starttime);
            $where['insertymd'] = array('between',array(Common::toDate($starttime[0],'Ymd'),Common::toDate($starttime[1],'Ymd')));
        }

        $uriModule = new UriModule();
        $list = $uriModule->getUriList($where, '*', 'id desc', $page, $pageSize);
        $total = $uriModule->getUriList($where, 'count(1) as total');
        $total = $total ? intval($total[0]['total']) : 0;
        foreach ($list as &$val){
            if (!empty($val['url'])){
                $url = "{$this->url}/{$val['key']}";
                $val['key'] = '<a href="'.$url.'" target="_blank">'.$url.'</a>';
            }else{
                $val['key'] = '';
            }
            $val['inserttime'] = Common::toDate($val['inserttime']);
            $val['updatetime'] =  Common::toDate($val['inserttime']);
//            $val['operate'] = '<a href="javascript:;" onclick="show_task_result(this);" data-id="'.$val['id'].'" class="layui-table-link" data-title="查看分析结果-'.$val['id'].'">查看分析结果</a>';
        }

        $result = array(
            'code' => 0,
            'count' => $total,
            'list' => $list,
        );

        $this->ajaxReturn($result);
    }

    public function doAdd(){
        if ($this->isPost()) {
            $title = AppCommon::getRequestString('title');
            $url = AppCommon::getRequestString('url');
            if (empty($url)){
                $this->ajaxReturn(null, 400, '跳转地址不能为空');
            }
            if (!filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)){
                $this->ajaxReturn('', 0, '跳转地址必须为正确完整的URL地址!');
            }
            $time = time();
            $data = array(
                'title' => $title,
                'url' => $url,
                'adminid' => $this->userinfo['adminid'],
                'adminname' => $this->userinfo['account'],
                'inserttime' => $time,
                'insertymd' => date('Ymd'),
            );
            $uriModule = new UriModule();
            $insertId = $uriModule->add($data);
            if ($insertId === false) {
                $this->ajaxReturn(null, 400, '创建失败');
            }
            //生成key
            $key = UrlUtil::createShortKey($insertId);
            $uriModule->saveUri($insertId,array('key'=>$key));
            $this->ajaxReturn(null, 1, '操作成功');
        }

        $this->display("uri/add.html");
    }

    public function doIndexPv(){
        $this->smarty->display('uri/index_pv.html');
    }
    public function pvList(){
        $pageSize = AppCommon::getRequestInt("limit");
        $page = AppCommon::getRequestInt("page");
        $pageSize = $pageSize > 0 ? $pageSize : 10;
        $respage = $page > 0 ? $page : 1;
        $page = ($respage - 1) * $pageSize;

        $uri_key = AppCommon::getRequestString('uri_key');
        $starttime = AppCommon::getRequestString('starttime');

        $where = array();

        if ($uri_key){
            $where['key'] = $uri_key;
        }
        if ($starttime){
            $starttime = explode(' - ',$starttime);
            $where['insertymd'] = array('between',array(Common::toDate($starttime[0],'Ymd'),Common::toDate($starttime[1],'Ymd')));
        }

        $uriModule = new UriModule();
        $list = $uriModule->getUriPvList($where, '*', 'id desc', $page, $pageSize);
        $total = $uriModule->getUriPvList($where, 'count(1) as total');
        $total = $total ? intval($total[0]['total']) : 0;
        foreach ($list as &$val){
            $val['inserttime'] = Common::toDate($val['inserttime']);
        }

        $result = array(
            'code' => 0,
            'count' => $total,
            'list' => $list,
        );

        $this->ajaxReturn($result);
    }

}